﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;


namespace Marmat.Forms
{
    internal class DataTypeConverter : TypeConverter
    {
        private StandardValuesCollection _values;
        private Dictionary<Type, string> _names;

        public DataTypeConverter()
        {
            this._names = new Dictionary<Type, string>();
            this._names.Add(typeof(string), "String");
            this._names.Add(typeof(bool), "Boolean");
            this._names.Add(typeof(DateTime), "DateTime");
            this._names.Add(typeof(Color), "Color");
            this._names.Add(typeof(decimal), "Decimal");
            this._names.Add(typeof(double), "Double");
            this._names.Add(typeof(float), "Single");
            this._names.Add(typeof(short), "Short");
            this._names.Add(typeof(int), "Integer");
            this._names.Add(typeof(object), "Object");
            this._names.Add(typeof(byte), "Byte");
            this._names.Add(typeof(byte[]), "Byte[]");
        }

        internal string TypeToString(Type type)
        {
            if (this._names.ContainsKey(type))
            {
                return this._names[type];
            }

            return type.ToString();
        }

        internal Type StringToType(string typeName)
        {
            foreach (Type k in this._names.Keys)
            {
                if ((this._names[k] ?? "") == (typeName ?? ""))
                {
                    return k;
                }
            }

            return Type.GetType(typeName, true);
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type type)
        {
            return type.Equals(typeof(string)) || base.CanConvertFrom(context, type);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                return this.StringToType(Convert.ToString(value));
            }

            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType is null)
            {
                throw new ArgumentNullException("destinationType");
            }

            if (ReferenceEquals(destinationType, typeof(string)) && value is Type)
            {
                return this.TypeToString((Type)value);
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return false;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            if (this._values is null)
            {
                var array = new Type[] { typeof(string), typeof(bool), typeof(DateTime), typeof(Color), typeof(decimal), typeof(double), typeof(float), typeof(short), typeof(byte), typeof(int), typeof(object), typeof(byte[]) };
                this._values = new StandardValuesCollection(array);
            }

            return this._values;
        }
    }
}